package org.papervision3d.core.animation.track
{
	import flash.errors.IllegalOperationError;
	
	import org.papervision3d.core.animation.keyframe.AbstractKeyFrame;
	import org.papervision3d.objects.DisplayObject3D;

	/**
	 * 
	 * 
	 * @author Tim Knip
	 */ 
	public class MatrixStackTrack extends AbstractTrack
	{
		private var _tracks :Array;
		
		public function MatrixStackTrack(target:DisplayObject3D)
		{
			super(target);
			_tracks = new Array();
		}
		
		/**
		 * 
		 */ 
		public override function addKeyFrame(keyframe:AbstractKeyFrame):void
		{	
			throw new IllegalOperationError("You can't add keyframes to a MatrixStackTrack!");
		}
		
		/**
		 * 
		 */ 
		public function addTrack(track:AbstractTrack):void
		{
			if(_tracks.indexOf(track) == -1)
			{
				_tracks.push(track);		
				startTime = Math.min(startTime, track.startTime);
				endTime = Math.max(endTime, track.endTime);
			}
		}
		
		public override function tick(seconds:Number):void
		{
			if(!_tracks || !_tracks.length) return;
			
			var i :int;
			var track :AbstractTrack;
			
			matrix.reset();
			
			for(i = 0; i < _tracks.length; i++)
			{
				track = _tracks[i];
				
				track.tick(seconds);	
				
				matrix.calculateMultiply(matrix, track.matrix);
			}	
			
			target.copyTransform(matrix);	
		}
	}
}